<?php
// +----------------------------------------------------------------------
// | 日历类 (for thinkphp)
// +----------------------------------------------------------------------
// | @link ( http://www.yurnero.net )
// +----------------------------------------------------------------------
// | @copyright
// +----------------------------------------------------------------------
// | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | @author Haijun Wu <nicholasinlove@126.com>
// +----------------------------------------------------------------------
// | $Id: Calendar.class.php 83 2011-04-01 16:59:50Z nicholasinlove1986@gmail.com $
// +----------------------------------------------------------------------

/**
 +------------------------------------------------------------------------------
 * 使用实例
 +------------------------------------------------------------------------------
 * import("@.ORG.Calendar");	
 * 当天时间
 * $y = Calendar::local_date('Y');
 * $m = Calendar::local_date('m');
 * $date = new Calendar($y,$m);		
 * $date->outPut(&$num);
 +------------------------------------------------------------------------------
 */
 
class Calendar extends Think {

	public $year;
	public $month;
	public $day;
	public $theFirstWeekDay;
	public $theLastDay;
	public $lastYear;
	public $lastMonth;
	public $nextMonth;
	public $nextYear;
	public $monthName = array('','January','February','March','April','May','June','July','August','September','October','November','December');
	public $monthShortName = array('','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

	public function __construct($y,$m) {
	    $this->year             =  $y;
		$this->month            =  $m;
		$this->day              =  $this->local_date('d');    
		$this->theFirstWeekDay  =  $this->local_date('w',mktime(0,0,0,$this->month,1,$this->year));
		$this->theLastDay       =  $this->local_date('t',mktime(0,0,0,$this->month,1,$this->year));
		$this->getDateOffset($this->year,$this->month);		
	}
	
	/**
     +----------------------------------------------------------
     * 获取 (日期偏移)上一个月年份、上一个月、下一个月、下一个月年份
     +----------------------------------------------------------
     * @access private
     * @param integer $y 年份
     * @param integer $m 月份
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     */
	private function getDateOffset($y,$m) {
		if ($m == 1) {
			$lastYear = $y-1;
			$lastMonth = 12;
			$nextMonth = $m+1;
			$nextYear = $y;
		} elseif ($m == 12) {
			$lastYear = $y;
			$lastMonth = $m - 1;
			$nextYear = $y + 1;
			$nextMonth = 1;
		} else {
			$lastMonth = $m - 1;
			$nextMonth = $m + 1;
			$lastYear = $nextYear = $y;
		}
		if ($nextMonth < 10) $nextMonth = '0'.$nextMonth;
		if ($lastMonth < 10) $lastMonth = '0'.$lastMonth;
	
		$this->lastYear         =  $lastYear;
		$this->lastMonth        =  $lastMonth;
		$this->nextMonth        =  $nextMonth;
		$this->nextYear         =  $nextYear;
	}
	
	/**
     +----------------------------------------------------------
     * 获取日期，以二维数组的形式返回
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @return array
     +----------------------------------------------------------
     */
	public function getDates() {
		$dayRow = 1;
		while($dayRow <= $this->theFirstWeekDay) {
			$day[] = '&nbsp;';
			$dayRow++;
		}
		$i = 1;
		while($i <= $this->theLastDay){
			$day[] = $i;
			if(($dayRow % 7) == 0) {
				$date[] = $day;
				unset($day);
			}
			$i++;
			$dayRow++;
		}
		$last_element = $date[count($date)-1][6];
		$j = $last_element;
		for($i=1; $i<=$this->theLastDay - $last_element; $i++) {
			$add_date[] = $j+1;
			$j++;
		}
		for($i=1; $i<=7 - ($this->theLastDay - $last_element); $i++){
			$add_date[] = '&nbsp;';
		}
		if(isset($add_date)) {
			$date[] = $add_date;
		}
		return $date;
	}
	
	public function outPut(&$num) {
		$ret['html'] = $this->makeHtml(&$num);
		$ret['prev'] = $this->lastYear.'/'.$this->lastMonth;
		$ret['prevyear'] = $this->lastYear.'年';
		$ret['prevmonth'] = $this->lastMonth.'月';
		$ret['next'] = $this->nextYear.'/'.$this->nextMonth;
		$ret['nextyear'] = $this->nextYear.'年';
		$ret['nextmonth'] = $this->nextMonth.'月';
		//$ret['now_date']  = $this->year.'年'.$this->month.'月';
		$e_month   = ($this->month < 10) ? str_replace('0', '', $this->month) : $this->month;
		$p_e_month = ($this->lastMonth < 10) ? str_replace('0', '', $this->lastMonth) : $this->lastMonth;
		$n_month   = ($this->nextMonth < 10) ? str_replace('0', '', $this->nextMonth) : $this->nextMonth;
		$ret['now_date']  = $this->monthName[$e_month].' '.$this->year;
		$ret['toprev'] = $this->monthShortName[$p_e_month];
		$ret['tonext'] = $this->monthShortName[$n_month];
		return $ret;
	}
	
	public function makeHtml(&$num) {
	    $html = '';
		$date = $this->getDates();
		foreach($date as $val){
			$html .= "       <tr>\n\t";
			foreach($val as $day){
				if($this->day == $day && $this->year == $this->local_date('Y') && $this->month == $this->local_date('m')){
					$html .= "<td id='today'>";
					if(isset($num[$day])) {
						$html .= "<a href=\"".__APP__."/Date/".$this->year."/".$this->month."/".$day."\" title=\"".$day." 日发表了 ".$num[$day]." 篇日志\">".$day."</a>";
					} else {
						$html .= $day;
					}
					$html .= "</td>";
				}else{
					$html .= "<td>";
					if(isset($num[$day])) {
						$html .= "<a href=\"".__APP__."/Date/".$this->year."/".$this->month."/".$day."\" title=\"".$day." 日发表了 ".$num[$day]." 篇日志\">".$day."</a>";
					} else {
						$html .= $day;
					}
					$html .= "</td>";
				}
			}
			$html .= "\n\t</tr>\n";
		}
		return $html;
	}
	
	/**
	 * 获得当前格林威治时间的时间戳
	 *
	 * @return  integer
	 */
	static public function gmtime() {
		return (time() - date('Z'));
	}
	
	/**
	 * 将GMT时间戳格式化为用户自定义时区日期
	 *
	 * @param  string       $format
	 * @param  integer      $time       该参数必须是一个GMT的时间戳
	 *
	 * @return  string
	 */
	
	static public function local_date($format, $time = NULL) {  
		$timezone = C('server_timezone')? C('server_timezone') : '8';
		if ($time === NULL) {
			$time = gmtime();
		} elseif ($time <= 0) {
			return '';
		}
		$time += ($timezone * 3600);
		return gmdate($format, $time);
	}
	
}

?>